﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Threading;
using PMSGSC.Common;
using PMSGSC.DAL;
using PMSGSC.BLL;

namespace PMSGSC.App
{
    public partial class frmFindOnTree : DevExpress.XtraEditors.XtraForm
    {
        public frmFindOnTree()
        {
            InitializeComponent();
        }
        frmComponentHierachy objEquip;
        frmProcessBar pb = new frmProcessBar();
        private bool isStop = true;
        public frmFindOnTree(frmComponentHierachy eq)
        {
            InitializeComponent();
            objEquip = eq;
            txtInfo.Focus();
        }
        private void btnOKFind_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                if (txtInfo.Text != "   .   ." && txtInfo.Text != "")
                {

                    Thread thread = new Thread(new ThreadStart(() =>
                    {
                        LoadSkinToForm();
                        pb.ShowDialog();
                    }
                    ));

                    Equipment equip = Equipment_BLL.GetDataByNumber(txtInfo.Text);
                    if (null != equip)
                    {
                        isStop = false;
                        thread.Start();
                        objEquip.FindComponent(txtInfo.Text, radioGroup1.SelectedIndex);
                        if (pb.InvokeRequired)
                        {
                            pb.BeginInvoke(new Action(() => pb.Close()));
                             isStop = true;
                        }
                    }
                    else
                    {
                        XtraMessageBox.Show("No result in display", "Infomation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    XtraMessageBox.Show("Please enter infomation search component into Textbox", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }

        }


        private static void LoadSkinToForm()
        {
            DevExpress.Skins.SkinManager.EnableFormSkins();
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.UserSkins.OfficeSkins.Register();
            DevExpress.LookAndFeel.DefaultLookAndFeel obj = new DevExpress.LookAndFeel.DefaultLookAndFeel();
            try
            {
                LookAndFeelSettings.Load("LookAndFeelSettings.dat");
            }
            catch
            {
                obj.LookAndFeel.SetSkinStyle("Office 2007 Blue");
            }
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                txtInfo.Mask = "000.000.000";
                labelControl2.Text = "Number :";
                txtInfo.Focus();
            }
            else
            {
                txtInfo.Mask = "";
                labelControl2.Text = "Name :";
                txtInfo.Focus();
            }
        }

        private void btnCancelFind_Click(object sender, EventArgs e)
        {
            if (isStop)
            {
                this.Close();
            }
        }
       
    }
}